package com.embhealth.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.embhealth.entity.Result;
import com.embhealth.service.MemberExcelService;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/excelmember")
public class MemberExcelController {
    @Reference
    private MemberExcelService memberExcelService;

    Map<String, Object> result = null;

    /**
     * 获取到选中的id
     *
     * @param ids
     * @return
     */

    @RequestMapping("/id_excel")
    public Result id_excel(@RequestBody HashMap<String,ArrayList<Integer>> ids) {
        try {
            memberExcelService.id_member(ids.get("ids"));
            return new Result(true, "添加选中成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "添加选中失败");
        }
    }

    /**
     * 导出excel数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/member_excel")
    public Result exportBusinessReport(HttpServletRequest request, HttpServletResponse response) {
        try {
            List<Map<String, Object>> result = memberExcelService.getMemberData();
            //动态获取模板在磁盘绝对路径
            String filePath = request.getSession().getServletContext().getRealPath("/template") + File.separator + "member.xlsx";
            //在基于excel模板文件
            XSSFWorkbook excel = new XSSFWorkbook(new FileInputStream(new File(filePath)));
            //读取第一个工作表

            XSSFSheet sheet = excel.getSheetAt(0);
            //向模板填充数据
            int rowNum = 1;
            for (Map<String, Object> map : result) {
                XSSFRow row = sheet.createRow(rowNum++);
                XSSFCell cell = row.createCell(0);
                cell.setCellValue((String) map.get("mname"));
                cell = row.createCell(1);
                cell.setCellValue((String) map.get("msex"));
                cell = row.createCell(2);
                cell.setCellValue((String) map.get("mphoneNumber"));
                cell = row.createCell(3);
                cell.setCellValue((String) map.get("sname"));
            }
            //创建输出流，用于从服务器写数据到客户端浏览器
            ServletOutputStream out = response.getOutputStream();
            response.setContentType("application/vnd.ms-excel");//代表是Excel类型
            response.setHeader("content-Disposition", "attachment;filename=member.xlsx");//指定以附件形式下载
            excel.write(out);

            out.flush();
            out.close();
            excel.close();
            return new Result(true, "批量导出操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "批量导出操作失败");
        }

    }

}
